﻿Imports System.Data.SqlServerCe
Imports Framework.Data

Public Class DownloadsItemsEx
    Shared Sub New()
        Dim aSelectColumns As String() = {"Id", "Name", "UniqueId" _
                                        , "ItemType" _
                                        , "Priority", "Comment"}
        ObjectPersister.DefineObject(GetType(Entities.Downloads.Item), "Downloads_Items", aSelectColumns)
    End Sub
    Private Sub New()
    End Sub

    Public Shared Function GetAll() As Generic.List(Of Entities.Downloads.Item)
        Return ObjectPersister.GetAll(Of Entities.Downloads.Item)()
    End Function
    Public Shared Function GetByUniqueId(ByVal uniqueId As String) As Entities.Downloads.Item
        Dim strClause As String = "UniqueId=@UniqueId"
        Dim sqlPar1 As New SqlCeParameter("@UniqueId", uniqueId)
        Return ObjectPersister.GetOne(Of Entities.Downloads.Item)(strClause, sqlPar1)
    End Function
    Public Shared Function GetById(ByVal id As Integer) As Entities.Downloads.Item
        Dim strClause As String = "Id=@Id"
        Dim sqlPar1 As New SqlCeParameter("@Id", id)
        Return ObjectPersister.GetOne(Of Entities.Downloads.Item)(strClause, sqlPar1)
    End Function
    Public Shared Function GetById(ByVal id As Integer, ByVal transactionScope As TransactionScope) As Entities.Downloads.Item
        Dim strClause As String = "Id=@Id"
        Dim sqlPar1 As New SqlCeParameter("@Id", id)
        Return ObjectPersister.GetOne(Of Entities.Downloads.Item)(strClause, transactionScope, sqlPar1)
    End Function

    Public Shared Sub Save(ByVal item As Entities.Downloads.Item, ByVal transactionScope As TransactionScope)
        Dim clsCmd As New SaveCommand(Of Integer)("Downloads_Items", "Id", item.Id, 0)
        clsCmd.AddParam("Name", item.Name, False)
        clsCmd.AddParam("UniqueId", item.UniqueId, False)
        clsCmd.AddParam("ItemType", item.ItemType, False)
        clsCmd.AddParam("Priority", item.Priority, False)
        clsCmd.AddParam("Comment", item.Comment, True)
        item.Id = clsCmd.Save(transactionScope)
    End Sub

    Public Shared Sub Delete(ByVal id As Integer, ByVal transactionScope As TransactionScope)
        Dim strSql As String = "DELETE FROM Downloads_Items WHERE [Id]=@Id"
        Dim sqlCmd As Command = Environment.NewCommand(strSql)
        sqlCmd.AddParameterWithValue("@Id", id)
        Environment.ExecuteNonQuery(sqlCmd)
    End Sub

End Class
